package com.darco.darcoapp;

import com.darco.darcoapp.db.DarcoData;

import eneter.messaging.endpoints.typedmessages.TypedResponseReceivedEventArgs;
import DARCOShared.requests.GetCardImageRequest;
import DARCOShared.responses.GetCardImageResponse;
import DARCOShared.responses.ResponseBase;
import DARCOShared.responses.ResponseType;
import DARCOShared.responses.ResponseWrapper;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;
import static DARCOShared.utils.Constants.*;

public class Image_Activity extends MessagingActivity implements OnClickListener {
	private MessagingManager mm;
	private DarcoData d;
	private Bundle extra=null;
	private ContentValues cardInfo=null;
	private byte[] image=null;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_image_);

		extra= getIntent().getExtras();

		ImageView imageV= (ImageView) findViewById(R.id.image_imageView);
		imageV.setOnClickListener(this);

		d= new DarcoData(this);
		
		if(extra!=null){
			cardInfo = (ContentValues) extra.get("cardInfo");
			image= d.getCardImageFromLocalDB(cardInfo.getAsString(CARD_SN));
			
			if(image==null){
				//immagine da richiedere
				GetCardImageRequest reqImage= new GetCardImageRequest(cardInfo.getAsString(CARD_SN));
				onPerformRequest(reqImage);
			}
			else{
				//immagine gia presente nel Bundle
				Bitmap bmp=BitmapFactory.decodeByteArray(image,0,image.length);
				if(bmp!=null){
					imageV.setImageBitmap(bmp);
					imageV.setContentDescription(cardInfo.getAsString(CARD_SN));
				}	
			}
		}
		else{
			Toast.makeText(this, "Error: no card reference found", Toast.LENGTH_SHORT).show();
		}





	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		//il menu non serve
		// Inflate the menu; this adds items to the action bar if it is present.
		//getMenuInflater().inflate(R.menu.activity_image_, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()){
		case R.id.image_imageView:
			Intent result = new Intent();
			result.putExtra("IMAGE", image);
			setResult(Activity.RESULT_OK, result);
			finish();//chiudo l'activity
		}

	}


	void onPerformRequest(GetCardImageRequest req){
		//inizializzazione ...
		mm = new MessagingManager();
		mm.ConnectToServer(this); //mi connetto al server e mi iscrivo alle risposte
		mm.SendRequest(req, this); //invia una richiesta di info carta
		//qui sara necessario mostrare un popup di attesa, tipo "caricamento in corso" o roba simile
		//Toast.makeText(getApplicationContext(), "Request sent!", Toast.LENGTH_SHORT).show();
		showLoadingDialog("Waiting for image data");
	}


	@Override
	void onResponseReceived(Object sender,
			TypedResponseReceivedEventArgs<ResponseWrapper> e) {
		ResponseBase respbase = e.getResponseMessage().getResponse();
		ResponseType resptype=respbase.getResponseType();
		switch(resptype){
		case GetCardImage:
			GetCardImageResponse respImage= (GetCardImageResponse) respbase;
			//inserisco l'immagine nel ImageViewer
			ImageView imV= (ImageView)findViewById(R.id.image_imageView);
			image = respImage.cardImage;
			//inserimento nel db
			d.updateCardImageInLocalDB(cardInfo.getAsString(CARD_SN), image);
			Bitmap bmp=BitmapFactory.decodeByteArray(image,0,image.length);
			if(bmp!=null){
				imV.setImageBitmap(bmp);
				imV.setContentDescription(cardInfo.getAsString(CARD_SN));
			}
			hideLoadingDialog();
			break;
		default:
			break;
		}


	}

}
